package cn.shop.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.shop.admin.common.util.CurrentUserUtil;
import cn.shop.admin.service.IpWhitelistService;
import cn.shop.transcation.ipWhitelistModule.entity.IpWhitelist;
import cn.shop.transcation.ipWhitelistModule.mapper.IpWhitelistMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ip 白名单(IpWhitelist)表服务实现类
 *
 * @author jzw
 * @since 2022-02-23 11:04:03
 */
@Service("ipWhitelistService")
public class IpWhitelistServiceImpl extends ServiceImpl<IpWhitelistMapper, IpWhitelist> implements IpWhitelistService {

	@Resource
	private IpWhitelistMapper ipWhitelistMapper;
	/**
	 * 设置白名单
	 *
	 * @param ipList
	 * @return
	 * @author jzw
	 * @since 2022/2/23 11:19
	 */
	@Override
	public boolean insert(List<String> ipList) {
		// 删除当前用户下所有白名单
		LambdaQueryWrapper<IpWhitelist> query = Wrappers.lambdaQuery();
		Long userId = CurrentUserUtil.getUserId();
		query.eq(IpWhitelist::getUserId, userId);
		baseMapper.delete(query);
		ipList.removeIf(StrUtil::isBlank);
		List<IpWhitelist> collect = ipList.stream().map(e -> {
			IpWhitelist ipWhitelist = new IpWhitelist();
			ipWhitelist.setId(IdWorker.getId());
			ipWhitelist.setIp(e);
			return ipWhitelist;
		}).collect(Collectors.toList());
		if (collect.isEmpty()) {
			return true;
		}
		//添加白名单
		return SqlHelper.retBool(baseMapper.save(collect, userId));
	}

	/**
	 * 清除数据
	 *
	 * @param clearTime
	 * @return boolean
	 * @author 王鹤然
	 * @date 2023/1/30 0030 上午 11:50
	 */
	@Override
	@Transactional
	public boolean clearData(Object clearTime) {
		//资金管理 fund_detail record withdraw order
		//用户提现
		//日志 back_log
		return ipWhitelistMapper.clearData(clearTime);
	}
}

